package org.fuys.etp.core.metrics.impl.trend.type.impl;

import org.fuys.etp.core.metrics.impl.trend.type.StageJudgeTypeProcessor;
import org.fuys.etp.core.model.type.EtpTaskTrendTypeEnum;
import org.fuys.etp.core.model.vo.TpConfigVO;
import org.fuys.etp.core.reject.EtpRejectHandler;
import org.springframework.stereotype.Component;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @projectName: fuys-etp
 * @package: org.fuys.etp.core.metrics.fixed
 * @className: RejectCountHandler
 * @author: WangJing
 * @description: 拒绝处理次数处理器
 * @date: 2025/2/8 18:36
 * @version: 1.0
 */
@Component("rejectCountHandle")
public class RejectCountHandler implements StageJudgeTypeProcessor {
    @Override
    public Integer judge(boolean expansion, String compareType, ThreadPoolExecutor poolExecutor, TpConfigVO configVO) {
        final RejectedExecutionHandler rejectedExecutionHandler = poolExecutor.getRejectedExecutionHandler();
        if(rejectedExecutionHandler instanceof EtpRejectHandler){
            EtpRejectHandler countHandler = (EtpRejectHandler) rejectedExecutionHandler;
            final long count = countHandler.rejectCount();
            Integer target = expansion?configVO.getFixedConfig().getExpansionNum():
                    configVO.getFixedConfig().getScaleNum();
            return this.compare(compareType, Double.parseDouble(target.toString())
                    , Double.parseDouble(Long.toString(count)),expansion);
        }
        return EtpTaskTrendTypeEnum.UNABLE.getType();
    }
}
